package com.pray.praymall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.pray.praymall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private String app_id = "2016102500760568";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCZ7uQn9FMCGnXkfb8tgzWXz/kjW6qtkH6QYoz1eXpJb5zpUrp3ehtBF7c/Tlk6nwUf4e/zkXkFoZI+haBsG3uwjCJn91+IVFYdyuh7nkq91d+EjKPGtYvDOvVAB3BCYF5RSG31kFp+XktRiC93wI/2Eo190nm7nz1ngPFW7tvg33WeRcKKEfpmOWkm3v/O7EWdyJ8C6h1xv9O97epAeeVCG5tXxKdmUlDD1QrkjI/0eW7DLJBLREaukPQfbZ7ERlznw86S2AeOsWANBFZbgvDt5/1Z/d0n16R3nXxLH+AbubMorMbNFJ/vPVkYJ9ldqIiVtuD+QO2op4VwcKZD8ZIPAgMBAAECggEABiiWnDqcL5NbcB7PzURYmpBJiW6bb4I+bgxjU1be251Ji9QxfaI3AO2v+SprS3sdO19QuE/zpZQHQ9UYy/p1gmS446vz+IJXR63ci0WT+/BYlvcBfmu5AcCUG1Gw3hb47wuUyqRR2ohvPv3R2Fqt58xqOMR49layoDd6cF3yEhcOk5UgazNUHNpoRnkMF9WKJqgAqrg3nAhzyNzYXTh2FhehD7NaHAOZMTc01SfKH1XATdabgSMUIbhC1eyC6McwpWEGykK+J/o+sQ8YlHi9ErRQz2U4Ptf0Z/+jl22kbsWYdKT7CuPAWNXtfOmxkwrrQtfRohvsnP9W2KDQU0Z1MQKBgQDlBrJNFC5S+ehHgliPLVed+J8OJ2dJ+3uNLTugbqhvXYA+rxTVKm0mpCvlv3JS8yPpXfvfKMZVo6gYTStHMKtxpdzZhHn8Dv43Vhou78h9L1hlRy2ZKurDRFbSUYmzHSqS3Xm+sDMRO4BDipFEWGJL7UQcr+9hz4yZ8hQbm0rZEwKBgQCsEBZKRhKWBrmvtsBhUGJGxY/0LgY2DJ6CMI/NCP/Or+lbjYD5i5gcLhJkLpwp9ENlaTOvZvmej3XKOpj2ArRcQ38OgstbGVa7ClDm1BHIWdxiAv5ica19a+ZvfBoxP/jX0ymwcusBRMxIocfwI+I6iIODe6og8ejAJYvmO2selQKBgQCMG/2mU+tHDeXBIYfGB6orVTcQ9KAmzXe3VW295xSaFKxpLLjk87CUzCiiw+1Pu7lDgiCdVBeJurYSL9uDEM5Bl4O1emsZ/Z4eAknAXybbFsTQKKxHoM7AKKWowwMnlXc/I8jgFHEZYLpGF5ZpJlz0MDHUDvm2b7+HSmZswSMYCwKBgB7L2lF87UKw/Xc3zIMechicr8FSo4X1CEHhEzvtimuiDLB4SUdg9t7EWEJaHVVdBNsX00pnsfHxV3HUvx/5J/MDmbAtNNyfUmiz1fsapfwoKUvZoVJZamXydBIsMjvyaUWn4+RTDUDy4WV2n2SZ55n2aSOVdMPkyHXrwnh41hlpAoGBAMecq80gx1tFWktPArfybuOzP6CHd3Bs3o2VcaeQGsfvbdKrm6F2y0GO7HIayou6EAt8Z3aNZf3KiO7C7Jz+aCMNMu4aN7/08i6xmlfS0Mc4C+kF7nSioLPBkIP6w+/PEOBjpXqex8JmsNf9FAJmRKrv/ngqN22m3+OngWJDEqGa";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAo4RXevEsGFwcSiiiHsgoeY2xGjSZ75KwcV9i/p9AV+RdQIXJqDAeF6kqpw4tLeJAeQmbQuRb+a8MpZ9o0Pt5vQg7DEQpI++W1ahC8HPdkbETzeFnpEdiO6sYG+wj27+Ium6hBrrWbSJg9YoL9xiLYdAO1lUCR1gVONHSYMqxOKoQUA08bSPdmFqVx5wv9srBlFQIFuovDOv9mqUq9wzWsAAj+k7dbIU5L+ohV/yyVdz6WIHhIyStzeaoS8QU1BVkeWDyd5MXHVQBdjDAdsSYfER2lOvYWZm2Ym1kbOImC7cS8Luc9N+WekgCWd9chxy0Ps84Wo4DRsQy6fV3iNBhVwIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private String notify_url = "http://cn-zz-bgp-2.sakurafrp.com:50537/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private String return_url = "http://member.praymall.com/memberOrder.html";

    // 签名方式
    private String sign_type = "RSA2";

    // 字符编码格式
    private String charset = "utf-8";

    // 订单支付超时时间
    private String timeout = "30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"timeout_express\":\"+ timeout +\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应：" + result);

        return result;

    }
}
